<template>
	<view>
		<tito title="面对面扫码" :leftIconShow="true" />
		<view class="scrollView">
			<scroll-view class="scroll-view_H" scroll-x="true" scroll-left="0">
				<view class="scroll-list">
					<view class="scroll-view-item"
						:class="item.id == listID ? 'scroll-view-item_H' : 'scroll-view-item_N'"
						v-for="(item, index) in goodsList" :key="index" @click="getscroll(item, index)">
						{{item.name}}
					</view>
				</view>
			</scroll-view>
		</view>
		<view class="bj-qrsc">
			<image class="img" src="../../static/bg-jifen.png" mode=""></image>
			<view class="qr-text">
				<view class="qr">
					<image :src="qrCode" mode=""></image>
				</view>
				<view class="text">
					1.邀请好友注册成功，你们各自将获得{{registerIntegral}}积分
				</view>
				<view class="text">
					2.好友首次下单，你将再次获得{{firstOrderIntegral}}积分
				</view>
			</view>
		</view>
		<view class="buzou" @click="downloadFile">
			保存图片
		</view>
	</view>
</template>

<script>
	import {
		shareGoodsList,
		shareQrCode
	} from "@/api/api.js";
	export default {
		data() {
			return {
				registerIntegral: "",
				firstOrderIntegral: "",
				qrCode: "", //二维码
				listID: "0",
				goodsList: [],
			};
		},
		onLoad(ope) {
			if (ope.registerIntegral && ope.firstOrderIntegral) {
				this.registerIntegral = ope.registerIntegral
				this.firstOrderIntegral = ope.firstOrderIntegral
			}
			this.shareGoodsList()

		},
		methods: {
			//产品列表
			async shareGoodsList() {
				let res = await shareGoodsList()
				if (res.code == 0 && res.data.length > 0) {
					this.listID = res.data[0].id
					this.goodsList = res.data
					this.shareQrCode()
				}
			},
			getscroll(item, index) {
				this.listID = item.id
				this.shareQrCode()
			},
			//分享二维码
			async shareQrCode() {
				let res = await shareQrCode({
					goodsId: this.listID
				})
				if (res.data) {
					this.qrCode = res.data
				}
			},
			//保存图片
			downloadFile() {
				uni.downloadFile({
					url: this.qrCode,
					success: downloadResult => {
						if (downloadResult.statusCode === 200) {
							// 下载成功，保存图片到系统相册
							uni.saveImageToPhotosAlbum({
								filePath: downloadResult.tempFilePath,
								success: () => {
									uni.showToast({
										title: '图片保存成功'
									});
								},
								fail: () => {
									uni.showToast({
										title: '图片保存失败',
										icon: 'none'
									});
								}
							});
						} else {
							uni.showToast({
								title: '图片下载失败',
								icon: 'none'
							});
						}
					},
					fail: () => {
						uni.showToast({
							title: '图片下载失败',
							icon: 'none'
						});
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.scrollView {
		padding: 0rpx 30rpx;
		box-sizing: border-box;
		margin-bottom: 48rpx;

		.scroll-view_H {
			white-space: nowrap;
			width: 100%;
		}

		.scroll-list {
			display: flex;
			align-items: center;

			.scroll-view-item {
				margin-right: 24rpx;
				text-align: center;
				height: 72rpx;
				font-size: 28rpx;
				border-radius: 8rpx;
				padding: 0 32rpx;
				box-sizing: border-box;
			}

			.scroll-view-item_H {
				line-height: 72rpx;
				background: #E1F2FA;
				border: 2rpx solid #1DABF2;
				color: #1DABF2;
			}

			.scroll-view-item_N {
				line-height: 72rpx;
				background: #FFFFFF;
				color: #2E3133;
				border: 2rpx solid #FFF;
			}
		}
	}

	.bj-qrsc {
		width: 690rpx;
		height: 860rpx;
		margin: 0 auto;
		margin-top: 64rpx;
		position: relative;

		.img {
			position: absolute;
			width: 690rpx;
			height: 860rpx;
		}

		.qr-text {
			position: absolute;
			left: 50%;
			top: 168rpx;
			transform: translate(-50%, 0);

			.qr {
				image {
					width: 354rpx;
					height: 354rpx;
				}
			}

			.text {
				font-size: 28rpx;
				color: #FA6B05;
				margin-top: 24rpx;
			}
		}
	}

	.buzou {
		width: 690rpx;
		height: 88rpx;
		background: #2590FA;
		border-radius: 100rpx;
		position: fixed;
		bottom: 50rpx;
		margin: 0 30rpx;
		text-align: center;
		line-height: 88rpx;
		font-size: 32rpx;
		color: #FFFFFF;
	}
</style>